package Week7.J0528;

import java.util.Scanner;

public class 小红取数_xv {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        long[] arr = new long[n + 1];
        for (int i = 1; i <= n; i++) {
            arr[i] = scanner.nextLong();
        }

        long[][] dp = new long[n + 1][k];

        for (int i = 0; i <= n; i++) {
            for (int j = 0; j < k; j++) {
                dp[i][j] = (long) -1e16;
            }
        }
        dp[0][0] = 0;

        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < k; j++) {
                dp[i][j] = Math.max(dp[i - 1][j],
                        dp[i - 1][(int) ((j - arr[i] % (long) k + (long) k) % k)] + arr[i]);
            }
        }

        if (dp[n][0] > 0) System.out.println(dp[n][0]);
        else System.out.println(-1);
    }
}
